﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace Controller
{
    public class KPIStatusController : Controller.dbConnection.ConnectionClass
    {
        public KPIStatus GetKPIStatusByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetKPIStatusByID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            KPIStatus Status = null;

            if (Data != null && Data.Count > 0)
            {
                Status = new KPIStatus();
                Status.ID = Convert.ToInt16(Data[0]);
                Status.Description = Data[1].ToString();
                Status.isActive = Convert.ToBoolean(Data[2]);
                Status.LastModifiedDate = Data[3] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[3].ToString());
                Status.LastModifiedBy = Data[4].ToString();
                Status.CreateDate = Data[5] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[5].ToString());
                Status.CreateBy = Data[6].ToString();
            }

            return Status;
        }

        public IList<KPIStatus> GetAllKPIStatus()
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllKPIStatus";

            DataSet dx = GetTable(command);

            List<KPIStatus> Status = new List<KPIStatus>();

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        KPIStatus p = new KPIStatus();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        Status.Add(p);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return Status;
        }
    }
}